<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8" />
        <title>chapter9-3</title>
        <link rel="stylesheet" href="/theme/css/main.css" />

        <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
</head>

<body id="index" class="home">
        <header id="banner" class="body">
                <h1><a href="/">PythonTutorials </a></h1>
                <nav><ul>
                    <li class="active"><a href="/category/cpp.html">cpp</a></li>
                    <li><a href="/category/python.html">python</a></li>
                </ul>
                </nav>
        </header><!-- /#banner -->
<section id="content" class="body">
  <article>
    <header>
      <h1 class="entry-title">
        <a href="/chapter9-3.html" rel="bookmark"
           title="Permalink to chapter9-3">chapter9-3</a></h1>
    </header>

    <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/cpp.html">cpp</a><a href="/tag/progarmming.html">progarmming</a></span>
</footer><!-- /.post-info -->      <h1>1、如何创建单个节点</h1>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;stdlib.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string.h&gt;</span><span class="cp"></span>
<span class="cp">#define N 40</span>
<span class="k">typedef</span> <span class="k">struct</span> <span class="n">student</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">studentID</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">studentName</span><span class="p">[</span><span class="mi">20</span><span class="p">];</span>
    <span class="kt">char</span> <span class="n">studentSex</span><span class="p">;</span>

    <span class="k">struct</span> <span class="n">student</span> <span class="o">*</span><span class="n">next</span><span class="p">;</span>
<span class="p">}</span><span class="n">STUDENT</span><span class="p">;</span>

<span class="n">STUDENT</span> <span class="o">*</span> <span class="nf">Build</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>

    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">head</span><span class="p">;</span>
    <span class="n">head</span><span class="o">=</span><span class="n">Build</span><span class="p">();</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;the first student name is %s &quot;</span><span class="p">,</span> <span class="n">head</span><span class="o">-&gt;</span><span class="n">next</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">);</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>


<span class="n">STUDENT</span> <span class="o">*</span> <span class="nf">Build</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">stu1</span><span class="o">=</span><span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">stu2</span><span class="o">=</span><span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">stu3</span><span class="o">=</span><span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>
    <span class="k">if</span><span class="p">(</span><span class="n">stu1</span><span class="o">!=</span><span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">stu1</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="o">=</span><span class="mi">1001</span><span class="p">;</span>
        <span class="n">strcpy</span><span class="p">(</span><span class="n">stu1</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span><span class="s">&quot;马小安&quot;</span><span class="p">);</span>
        <span class="n">stu1</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="o">=</span><span class="sc">&#39;M&#39;</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span><span class="p">(</span><span class="n">stu2</span><span class="o">!=</span><span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">stu2</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="o">=</span><span class="mi">1002</span><span class="p">;</span>
        <span class="n">strcpy</span><span class="p">(</span><span class="n">stu2</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span><span class="s">&quot;胡小娜&quot;</span><span class="p">);</span>
        <span class="n">stu2</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="o">=</span><span class="sc">&#39;W&#39;</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span><span class="p">(</span><span class="n">stu3</span><span class="o">!=</span><span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">stu3</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="o">=</span><span class="mi">1003</span><span class="p">;</span>
        <span class="n">strcpy</span><span class="p">(</span><span class="n">stu3</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span><span class="s">&quot;马宇恒&quot;</span><span class="p">);</span>
        <span class="n">stu3</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="o">=</span><span class="sc">&#39;M&#39;</span><span class="p">;</span>
    <span class="p">}</span>


    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">head</span><span class="p">;</span>
    <span class="n">head</span><span class="o">=</span><span class="n">stu1</span><span class="p">;</span>
    <span class="n">stu1</span><span class="o">-&gt;</span><span class="n">next</span><span class="o">=</span><span class="n">stu2</span><span class="p">;</span>
    <span class="n">stu2</span><span class="o">-&gt;</span><span class="n">next</span><span class="o">=</span><span class="n">stu3</span><span class="p">;</span>
    <span class="n">stu3</span><span class="o">-&gt;</span><span class="n">next</span><span class="o">=</span><span class="nb">NULL</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">head</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<h1>2、批量创建结点</h1>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;stdlib.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string.h&gt;</span><span class="cp"></span>
<span class="cp">#define N 40</span>

<span class="k">typedef</span> <span class="k">struct</span> <span class="n">student</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">studentID</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">studentName</span><span class="p">[</span><span class="mi">20</span><span class="p">];</span>
    <span class="kt">char</span> <span class="n">studentSex</span><span class="p">;</span>
    <span class="k">struct</span> <span class="n">student</span> <span class="o">*</span><span class="n">next</span><span class="p">;</span>
<span class="p">}</span><span class="n">STUDENT</span><span class="p">;</span>

<span class="n">STUDENT</span> <span class="o">*</span> <span class="nf">Build</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">);</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">head</span><span class="p">;</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">p</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;pls input ID,name,Sex</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">head</span> <span class="o">=</span> <span class="n">Build</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">head</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">p</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d%8s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">);</span>
        <span class="n">p</span><span class="o">=</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span>
        <span class="n">j</span><span class="o">++</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">getchar</span><span class="p">();</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">STUDENT</span> <span class="o">*</span> <span class="nf">Build</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">Pt</span><span class="p">,</span> <span class="o">*</span><span class="n">prePt</span><span class="p">,</span> <span class="o">*</span><span class="n">head</span><span class="p">;</span>

    <span class="c1">//申请头结点</span>
    <span class="n">Pt</span> <span class="o">=</span> <span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">Pt</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">scanf</span><span class="p">(</span><span class="s">&quot;%d%s%c&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="p">,</span> <span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="p">);</span>
        <span class="n">getchar</span><span class="p">();</span><span class="c1">//读走缓存区的回车</span>
        <span class="n">head</span> <span class="o">=</span> <span class="n">Pt</span><span class="p">;</span>
        <span class="n">prePt</span> <span class="o">=</span> <span class="n">Pt</span><span class="p">;</span><span class="c1">//使用prePt来控制尾结点，而不是通过最后一个结点的指针变量来操作</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Failed</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="c1">//批量创建中间节点</span>
    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span><span class="n">num</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">Pt</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">student</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">student</span><span class="p">));</span>
        <span class="k">if</span><span class="p">(</span><span class="n">Pt</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&quot;pls input [%d]ID,name,Sex</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span>
            <span class="n">scanf</span><span class="p">(</span><span class="s">&quot;%d%s%c&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="p">,</span> <span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="p">);</span>
            <span class="n">getchar</span><span class="p">();</span><span class="c1">//读走缓存区的回车</span>
            <span class="n">prePt</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="n">Pt</span><span class="p">;</span><span class="c1">//新节点和之前的节点链接起来</span>
            <span class="n">prePt</span> <span class="o">=</span> <span class="n">Pt</span><span class="p">;</span><span class="c1">//prePt总是指向链表的尾节点</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Failed</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
            <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
        <span class="p">}</span>

    <span class="p">}</span>
    <span class="c1">//创建尾节点</span>
    <span class="n">prePt</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">head</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<h1>3、结点的遍历</h1>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;stdlib.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string.h&gt;</span><span class="cp"></span>
<span class="cp">#define N 40</span>

<span class="k">typedef</span> <span class="k">struct</span> <span class="n">student</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">studentID</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">studentName</span><span class="p">[</span><span class="mi">20</span><span class="p">];</span>
    <span class="kt">char</span> <span class="n">studentSex</span><span class="p">;</span>
    <span class="k">struct</span> <span class="n">student</span> <span class="o">*</span><span class="n">next</span><span class="p">;</span>
<span class="p">}</span><span class="n">STUDENT</span><span class="p">;</span>

<span class="n">STUDENT</span> <span class="o">*</span> <span class="nf">Build1</span><span class="p">();</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">head</span><span class="p">;</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">p</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="n">head</span> <span class="o">=</span> <span class="n">Build1</span><span class="p">();</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">head</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">p</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d%8s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">);</span>
        <span class="n">p</span><span class="o">=</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span>
        <span class="n">j</span><span class="o">++</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">getchar</span><span class="p">();</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">STUDENT</span> <span class="o">*</span> <span class="nf">Build1</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">stu1</span> <span class="o">=</span> <span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">stu2</span> <span class="o">=</span> <span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">stu3</span> <span class="o">=</span> <span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">stu1</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">stu1</span><span class="o">-&gt;</span><span class="n">studentID</span> <span class="o">=</span> <span class="mi">1001</span><span class="p">;</span>
        <span class="n">strcpy</span><span class="p">(</span><span class="n">stu1</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span> <span class="s">&quot;马小安&quot;</span><span class="p">);</span>
        <span class="n">stu1</span><span class="o">-&gt;</span><span class="n">studentSex</span> <span class="o">=</span> <span class="sc">&#39;M&#39;</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">stu2</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">stu2</span><span class="o">-&gt;</span><span class="n">studentID</span> <span class="o">=</span> <span class="mi">1002</span><span class="p">;</span>
        <span class="n">strcpy</span><span class="p">(</span><span class="n">stu2</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span> <span class="s">&quot;胡小娜&quot;</span><span class="p">);</span>
        <span class="n">stu2</span><span class="o">-&gt;</span><span class="n">studentSex</span> <span class="o">=</span> <span class="sc">&#39;W&#39;</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">stu3</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">stu3</span><span class="o">-&gt;</span><span class="n">studentID</span> <span class="o">=</span> <span class="mi">1003</span><span class="p">;</span>
        <span class="n">strcpy</span><span class="p">(</span><span class="n">stu3</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span> <span class="s">&quot;马宇恒&quot;</span><span class="p">);</span>
        <span class="n">stu3</span><span class="o">-&gt;</span><span class="n">studentSex</span> <span class="o">=</span> <span class="sc">&#39;M&#39;</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">head</span><span class="p">;</span>
    <span class="n">head</span> <span class="o">=</span> <span class="n">stu1</span><span class="p">;</span>
    <span class="n">stu1</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="n">stu2</span><span class="p">;</span>
    <span class="n">stu2</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="n">stu3</span><span class="p">;</span>
    <span class="n">stu3</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">head</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<h1>链表的查找、删除、插入</h1>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;stdlib.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string.h&gt;</span><span class="cp"></span>
<span class="cp">#define N 40</span>

<span class="k">typedef</span> <span class="k">struct</span> <span class="n">student</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">studentID</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">studentName</span><span class="p">[</span><span class="mi">20</span><span class="p">];</span>
    <span class="kt">char</span> <span class="n">studentSex</span><span class="p">;</span>
    <span class="k">struct</span> <span class="n">student</span> <span class="o">*</span><span class="n">next</span><span class="p">;</span>
<span class="p">}</span><span class="n">STUDENT</span><span class="p">;</span>
<span class="n">STUDENT</span> <span class="o">*</span><span class="n">head</span><span class="p">;</span>
<span class="n">STUDENT</span> <span class="o">*</span> <span class="nf">Build</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">);</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>

    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">p</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">name</span><span class="p">[</span><span class="mi">10</span><span class="p">];</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;pls input ID,name,Sex</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">head</span> <span class="o">=</span> <span class="n">Build</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">head</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">p</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d%8s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">);</span>
        <span class="n">p</span><span class="o">=</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span>
        <span class="n">j</span><span class="o">++</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">scanf</span><span class="p">(</span><span class="s">&quot;%s&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">name</span><span class="p">);</span>
    <span class="n">Find</span><span class="p">(</span><span class="n">name</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;The person %s will be deleted.</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="n">name</span><span class="p">);</span>
    <span class="n">Delete</span><span class="p">(</span><span class="n">name</span><span class="p">);</span>
    <span class="n">Print</span><span class="p">();</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">STUDENT</span> <span class="o">*</span> <span class="nf">Build</span><span class="p">(</span><span class="kt">int</span> <span class="n">num</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">Pt</span><span class="p">,</span> <span class="o">*</span><span class="n">prePt</span><span class="p">;</span>

    <span class="c1">//申请头结点</span>
    <span class="n">Pt</span> <span class="o">=</span> <span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">Pt</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">scanf</span><span class="p">(</span><span class="s">&quot;%d%s%c&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="p">,</span> <span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="p">);</span>
        <span class="n">getchar</span><span class="p">();</span><span class="c1">//读走缓存区的回车</span>
        <span class="n">head</span> <span class="o">=</span> <span class="n">Pt</span><span class="p">;</span>
        <span class="n">prePt</span> <span class="o">=</span> <span class="n">Pt</span><span class="p">;</span><span class="c1">//使用prePt来控制尾结点，而不是通过最后一个结点的指针变量来操作</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Failed</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="c1">//批量创建中间节点</span>
    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span><span class="n">num</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">Pt</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">student</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">student</span><span class="p">));</span>
        <span class="k">if</span><span class="p">(</span><span class="n">Pt</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&quot;pls input [%d]ID,name,Sex</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span>
            <span class="n">scanf</span><span class="p">(</span><span class="s">&quot;%d%s%c&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="p">,</span> <span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">Pt</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="p">);</span>
            <span class="n">getchar</span><span class="p">();</span><span class="c1">//读走缓存区的回车</span>
            <span class="n">prePt</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="n">Pt</span><span class="p">;</span><span class="c1">//新节点和之前的节点链接起来</span>
            <span class="n">prePt</span> <span class="o">=</span> <span class="n">Pt</span><span class="p">;</span><span class="c1">//prePt总是指向链表的尾节点</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Failed</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
            <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
        <span class="p">}</span>

    <span class="p">}</span>
    <span class="c1">//创建尾节点</span>
    <span class="n">prePt</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">head</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">Find</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">name</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">p</span><span class="p">,</span> <span class="o">*</span><span class="n">pr</span><span class="p">;</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">head</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">p</span> <span class="o">!=</span> <span class="nb">NULL</span> <span class="o">&amp;&amp;</span> <span class="n">strcmp</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">))</span>
    <span class="p">{</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">p</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Not Found!&quot;</span><span class="p">);</span>
        <span class="c1">//return 0;</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&quot;the student </span><span class="se">\&quot;</span><span class="s">%s</span><span class="se">\&quot;</span><span class="s"> number is %d.&quot;</span><span class="p">,</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="p">);</span>
        <span class="c1">//return 1;</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="n">Delete</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">name</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">p</span><span class="p">,</span> <span class="o">*</span><span class="n">pr</span><span class="p">;</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">head</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">strcmp</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span><span class="c1">//判断是否为头结点</span>
    <span class="p">{</span>
        <span class="n">head</span> <span class="o">=</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span>
        <span class="n">free</span><span class="p">(</span><span class="n">p</span><span class="p">);</span><span class="c1">//释放掉头结点</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="n">pr</span> <span class="o">=</span> <span class="n">p</span><span class="p">;</span><span class="c1">//前驱</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span><span class="c1">//后继</span>

        <span class="k">while</span> <span class="p">(</span><span class="n">p</span> <span class="o">!=</span> <span class="nb">NULL</span> <span class="o">&amp;&amp;</span> <span class="n">strcmp</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="n">pr</span> <span class="o">=</span> <span class="n">p</span><span class="p">;</span>
            <span class="n">p</span> <span class="o">=</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">p</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Not Found!&quot;</span><span class="p">);</span>
            <span class="c1">//return 0;</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="n">pr</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span>
            <span class="n">free</span><span class="p">(</span><span class="n">p</span><span class="p">);</span>
            <span class="c1">//return 1;</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="n">Print</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">p</span><span class="p">;</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">head</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">p</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%4d%8s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="p">);</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
    </div><!-- /.entry-content -->

  </article>
</section>
        <section id="extras" class="body">
                <div class="blogroll">
                        <h2>blogroll</h2>
                        <ul>
                            <li><a href="http://getpelican.com/">Pelican</a></li>
                            <li><a href="http://python.org/">Python.org</a></li>
                            <li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
                            <li><a href="#">You can modify those links in your config file</a></li>
                        </ul>
                </div><!-- /.blogroll -->
                <div class="social">
                        <h2>social</h2>
                        <ul>

                            <li><a href="#">You can add links in your config file</a></li>
                            <li><a href="#">Another social link</a></li>
                        </ul>
                </div><!-- /.social -->
        </section><!-- /#extras -->

        <footer id="contentinfo" class="body">
                <p>Powered by <a href="http://www.maxiaoan.com/">Maxiaoan</a></p>
        </footer><!-- /#contentinfo -->

</body>
</html>